home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap08.txt < prev    next >
Text File  |  1992-01-18  |  17KB  |  413 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 8
  8.                                                  MORE INHERITANCE
  9.  
  10. In the last chapter we developed a model using modes of
  11. transportation to illustrate the concept of inheritance.  In this
  12. chapter we will use that model to illustrate some of the finer
  13. points of inheritance and what it can be used for.  If it has been
  14. a while since you read and studied chapter 7, it would be good for
  15. you to return to that material and review it in preparation for a
  16. more detailed study of the topic of inheritance.
  17.  
  18.  
  19.  
  20. REORGANIZED FILE STRUCTURE
  21. _________________________________________________________________
  22.  
  23. A close examination of the file named            ================
  24. INHERIT1.CPP will reveal that it is identical to   INHERIT1.CPP
  25. the program developed in chapter 7 named         ================
  26. ALLVEHIC.CPP except that the program text is
  27. rearranged.  The biggest difference is that some
  28. of the simpler methods in the classes have been changed to inline
  29. code to shorten the file considerably.  In a practical programming
  30. situation, methods that are this short should be programmed inline
  31. since the actual code to return a simple value is shorter than the
  32. code required to send a message to a non-inline method.
  33.  
  34. The only other change is the reordering of the classes and
  35. associated methods with the classes all defined first, followed by
  36. the main program.  This puts all class interface definitions on a
  37. single page to make the code easier to study.  The implementations
  38. for the methods are deferred until the end of the file where they
  39. are available for quick reference but are not cluttering up the
  40. class definitions which we wish to study carefully in this chapter.
  41. This should be an indication to you that there is considerable
  42. flexibility in the way the classes and methods can be arranged in
  43. C++.  Of course you realize that this violates the spirit of C++
  44. and its use of separate compilation, but is only done here for
  45. convenience.  The best way to package all of the example programs
  46. in this chapter is like the packaging illustrated in chapter 7.
  47.  
  48. As mentioned before, the two derived classes, car and truck, each
  49. have a variable named passenger_load which is perfectly legal, and
  50. the car class has a method of the same name, initialize(), as one
  51. defined in the super-class named vehicle.  The rearrangement of the
  52. files in no way voids this allowable repeating of names.
  53.  
  54. After you have convinced yourself that this program is truly
  55. identical to the program named ALLVEHIC.CPP from chapter 7, compile
  56. and execute it with your compiler to assure yourself that this
  57.  
  58.                                                          Page 8-1
  59.  
  60.                                      Chapter 8 - More Inheritance
  61.  
  62. arrangement is legal.  Due to this means of code packaging, you
  63. will not need a "make" file or a "project" capability to compile
  64. and execute this code.  This is to make it easy to compile and
  65. execute the example programs in this chapter.
  66.  
  67.  
  68. THE SCOPE OPERATOR
  69. _________________________________________________________________
  70.  
  71. Because the method initialize() is defined in the derived car
  72. class, it hides the method of the same name which is part of the
  73. base class, and there may be times you wish to send a message to
  74. the method in the base class for use in the derived class object.
  75. This can be done by using the scope operator in the following
  76. manner in the main program;
  77.  
  78.    sedan.vehicle::initialize(4, 3500.0);
  79.  
  80. As you might guess, the number and types of parameters must agree
  81. with those of the method in the base class because it will respond
  82. to the message.
  83.  
  84.  
  85.  
  86. HIDDEN METHODS
  87. _________________________________________________________________
  88.  
  89. Examine the file named INHERIT2.CPP carefully    ================
  90. and you will notice that it is a repeat of the     INHERIT2.CPP
  91. last example program with a few minor changes.   ================
  92.  
  93. You will notice that the derived classes named
  94. car and truck do not have the keyword public prior to the name of
  95. the base class in the first line of each.  The keyword public, when
  96. included prior to the base class name, makes all of the methods
  97. defined in the base class available for use in the derived class
  98. just as if they were defined as part of the derived class.
  99. Therefore, in the previous program, we were permitted to call the
  100. methods defined as part of the base class from the main program
  101. even though we were working with an object of one of the derived
  102. classes.  One example of when we did this, was when we sent a
  103. message to the sedan to get its weight in an output statement of
  104. the main program.
  105.  
  106. In the present program, without the keyword public prior to the
  107. base class name, the only methods available for objects of the car
  108. class, are those that are defined as part of the class itself, and
  109. therefore we only have the methods named initialize() and
  110. passengers() available for use with objects of class car.  In this
  111. program, the only inheritance is that of variables since the two
  112. variables are inherited into objects of class car.
  113.  
  114. When we declare an object of type car, according to the definition
  115. of the C++ language, it contains three variables.  It contains the
  116.  
  117.                                                          Page 8-2
  118.  
  119.                                      Chapter 8 - More Inheritance
  120.  
  121. one defined as part of its class named passenger_load and the two
  122. that are part of its parent class, wheels and weight.  All are
  123. available for direct use within its methods because of the use of
  124. the keyword protected in the base class.  The variables are a part
  125. of an object of class car when it is declared and are stored as
  126. part of the object.  We will show you the details of access to the
  127. parent class variables within derived classes shortly in this
  128. chapter.  For now, we will return to the use of the subclasses in
  129. this example program.
  130.  
  131. The observant student will notice that several of the output
  132. statements have been commented out of the main program since they
  133. are no longer legal or meaningful operations.  Lines 57 through 59
  134. have been commented out because the methods named get_weight() and
  135. wheel_loading() are not inherited into the car class without the
  136. keyword public in the car class definition.  You will notice that
  137. initialize() is still available but this is the one in the car
  138. class, not the method of the same name in the vehicle class.
  139.  
  140. Moving on to the use of the truck class in the main program, we
  141. find that lines 63 and 65 are commented out for the same reason as
  142. given above, but lines 66 and 67 are commented out for an entirely
  143. different reason.  Even though the method named efficiency() is
  144. available and can be called as a part of the truck class, it cannot
  145. be used because we have no way to initialize the wheels or weight
  146. of the truck objects.  We can get the weight of the truck objects,
  147. as we have done in line 106, but since the weight has no way to be
  148. initialized, the result is meaningless and lines 66 and 67 are
  149. commented out.
  150.  
  151. As you have surely guessed by now, there is a way around all of
  152. these problems and we will cover them shortly.  In the meantime,
  153. be sure to compile and execute this example program to see that
  154. your compiler gives the same result.  It would be a good exercise
  155. for you to reintroduce some of the commented out lines to see what
  156. sort of an error message your compiler issues for these errors.
  157.  
  158.  
  159. INITIALIZING ALL DATA
  160. _________________________________________________________________
  161.  
  162. If you will examine the example program named    ================
  163. INHERIT3.CPP, you will find that we have fixed     INHERIT3.CPP
  164. the initialization problem that we left dangling ================
  165. in the last example program.
  166.  
  167. The method named init_truck() now contains all four of the
  168. parameters as input data which get transferred to the four
  169. variables.  Following the initialization, it is permissible to call
  170. the semi.efficiency() method in line 67 and 68 of the main program.
  171.  
  172. Be sure to compile and execute this pro